package com.lzc.jobrecomendbackend.service.impl;

import com.lzc.jobrecomendbackend.model.User;
import com.lzc.jobrecomendbackend.repository.UserRepository;
import com.lzc.jobrecomendbackend.service.SequenceGeneratorService;
import com.lzc.jobrecomendbackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private SequenceGeneratorService sequenceGeneratorService;
    
    private static final String USER_SEQUENCE_NAME = "user_sequence";

    @Override
    public User register(User user) {
        // 检查用户名是否已存在
        if (userRepository.existsByUsername(user.getUsername())) {
            throw new RuntimeException("用户名已存在");
        }
        
        // 检查邮箱是否已存在
        if (user.getEmail() != null && userRepository.existsByEmail(user.getEmail())) {
            throw new RuntimeException("邮箱已被注册");
        }
        
        // 设置创建时间和更新时间
        Date now = new Date();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        
        // 如果没有设置头像，提供默认头像
        if (user.getAvatar() == null || user.getAvatar().isEmpty()) {
            user.setAvatar("https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
        }
        
        // 生成自增ID
        user.setId(sequenceGeneratorService.generateSequence(USER_SEQUENCE_NAME));
        
        return userRepository.save(user);
    }

    @Override
    public User login(String username, String password) {
        Optional<User> userOpt = userRepository.findByUsername(username);
        if (!userOpt.isPresent()) {
            throw new RuntimeException("用户名不存在");
        }
        
        User user = userOpt.get();
        if (!password.equals(user.getPassword())) {
            throw new RuntimeException("密码错误");
        }
        
        return user;
    }

    @Override
    public User getUserInfo(String userId) {
        return userRepository.findById(userId)
                .orElseThrow(() -> new RuntimeException("用户不存在"));
    }

    @Override
    public User updateUserInfo(User user) {
        // 获取原用户信息
        User existingUser = userRepository.findById(user.getId())
                .orElseThrow(() -> new RuntimeException("用户不存在"));
        
        // 更新可修改的字段
        if (user.getEmail() != null) {
            existingUser.setEmail(user.getEmail());
        }
        if (user.getRealName() != null) {
            existingUser.setRealName(user.getRealName());
        }
        if (user.getAge() != null) {
            existingUser.setAge(user.getAge());
        }
        if (user.getGender() != null) {
            existingUser.setGender(user.getGender());
        }
        if (user.getEducation() != null) {
            existingUser.setEducation(user.getEducation());
        }
        if (user.getMajor() != null) {
            existingUser.setMajor(user.getMajor());
        }
        if (user.getExperience() != null) {
            existingUser.setExperience(user.getExperience());
        }
        if (user.getSkills() != null) {
            existingUser.setSkills(user.getSkills());
        }
        if (user.getPreferredCity() != null) {
            existingUser.setPreferredCity(user.getPreferredCity());
        }
        if (user.getPreferredPosition() != null) {
            existingUser.setPreferredPosition(user.getPreferredPosition());
        }
        if (user.getAvatar() != null) {
            existingUser.setAvatar(user.getAvatar());
        }
        
        // 更新修改时间
        existingUser.setUpdateTime(new Date());
        
        return userRepository.save(existingUser);
    }
} 